package com.thoughtworks.shoutbox.client.shouts;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.thoughtworks.shoutbox.client.dto.ShoutDTO;
import com.thoughtworks.shoutbox.client.services.ServiceCallback;
import com.thoughtworks.shoutbox.client.services.ShoutingService;
import com.thoughtworks.shoutbox.client.services.ShoutingServiceAsync;

public class ShoutListView extends Composite {
	private FlowPanel shoutHolder;
	private ShoutingServiceAsync service = GWT.create(ShoutingService.class);
	private String roomId;
	
	public ShoutListView() {
		FlowPanel rootPanel = new FlowPanel();
		shoutHolder = new FlowPanel();

		rootPanel.add(new Label("Shouts!"));
		rootPanel.add(shoutHolder);
		initWidget(rootPanel);
	}

	
	public void reloadShouts() {
		service.getAllShouts(roomId, new ServiceCallback<ArrayList<ShoutDTO>>() {
			public void onSuccess(ArrayList<ShoutDTO> shouts) {
				shoutHolder.clear();
				for (ShoutDTO shout : shouts) {
					shoutHolder.add(new Label(shout.getUserName()+" : "+shout.getMessage()));
				}
			}
			
		});
	}

	public void setRoomId(String roomId) {
		this.roomId = roomId;
	}

}
